package com.hdjk.view.activity.financial;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.liangmutian.mypicker.DateUtil;
import com.hdjk.view.R;
import com.hdjk.view.activity.main.LoginActivity;
import com.hdjk.view.activity.personalcenter.OpenAccountActivity;
import com.hdjk.view.callback.CallBackListener;
import com.hdjk.view.dialog.MyAlertDialog;
import com.hdjk.view.entity.FundEntity;
import com.hdjk.view.entity.Performance;
import com.hdjk.view.entity.PerformanceEntity;
import com.hdjk.view.network.FavoriteNetWork;
import com.hdjk.view.network.FundNetWork;
import com.hdjk.view.util.StringAppUtil;
import com.hdjk.view.util.ToastUtils;
import com.hdjk.view.util.UserTokenUtil;
import com.lst.projectlib.base.BaseActivity;
import com.lst.projectlib.entity.Msg;
import com.lst.projectlib.util.DensityUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 基金详情页
 * Created by sxy on 2017/11/2.
 */

public class FinancialDetailActivity extends BaseActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    public static final int TYPE_FINANCIAL = 1;
    public static final int TYPE_FUND = 2;

    private FundEntity mFund;
    // Content View Elements
    private com.lst.projectlib.component.CustomView.TopTitleBar ttb_title_bar;
    private TextView tv_name;
    private TextView tv_risk;
    private LinearLayout lin_number;
    private TextView tv_number;
    private TextView tv_type;
    private TextView tv_favorite;
    private TextView tv_new_value;
    private TextView tv_daily_increases;
    private TextView tv_date;
    private RadioGroup rg_movements;
    private RadioButton rb_earnings_movements, rb_value_movements, radio1, radio2, radio3, radio4, radio5, radio6;
    private RadioGroup rb_time;
    private TextView tv_more;
    private TextView tv_short_name;
    private TextView tv_fill_name;
    private TextView tv_company;
    private TextView tv_set_up_date;
    private TextView tv_fund_number;
    private TextView tv_fund_type;
    private TextView tv_fund_state;
    private TextView tv_trading_state;
    private TextView tv_management_fee;
    private TextView tv_trusteeship_fee;
    private TextView tv_first_collect_scale;
    private TextView tv_new_scale;
    private TextView tv_bank;
    private TextView tv_buy;
    private TextView tv_yifeng;
    private TextView tv_month1_1, tv_month1_2, tv_month1_3, tv_month3_1, tv_month3_2, tv_month3_3, tv_month6_1, tv_month6_2, tv_month6_3;
    private TextView tv_year_1, tv_year_2, tv_year_3, tv_year1_1, tv_year1_2, tv_year1_3, tv_year2_1, tv_year2_2, tv_year2_3, tv_year3_1, tv_year3_2, tv_year3_3;
    private LinearLayout lin_year;
    // End Of Content View Elements
    private WebView wv_chart;
    private String id, type;
    private JSONObject perjson_month, perjson_3month, perjson_year, perjson_3year, perjson_5year, perjson_history;
    private JSONObject hsjson_month, hsjson_3month, hsjson_year, hsjson_3year, hsjson_5year, hsjson_history;
    private JSONObject navjson_month, navjson_3month, navjson_year, navjson_3year, navjson_5year, navjson_history;

    private void bindViews() {
        ttb_title_bar = (com.lst.projectlib.component.CustomView.TopTitleBar) findViewById(R.id.ttb_title_bar);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_risk = (TextView) findViewById(R.id.tv_risk);
        lin_number = (LinearLayout) findViewById(R.id.lin_number);
        tv_number = (TextView) findViewById(R.id.tv_number);
        tv_type = (TextView) findViewById(R.id.tv_type);
        tv_favorite = (TextView) findViewById(R.id.tv_favorite);
        tv_new_value = (TextView) findViewById(R.id.tv_new_value);
        tv_daily_increases = (TextView) findViewById(R.id.tv_daily_increases);
        tv_date = (TextView) findViewById(R.id.tv_date);
        rg_movements = (RadioGroup) findViewById(R.id.rg_movements);
        rg_movements.setOnCheckedChangeListener(this);
        rb_earnings_movements = (RadioButton) findViewById(R.id.rb_earnings_movements);
        rb_value_movements = (RadioButton) findViewById(R.id.rb_value_movements);
        radio1 = (RadioButton) findViewById(R.id.radio1);
        radio2 = (RadioButton) findViewById(R.id.radio2);
        radio3 = (RadioButton) findViewById(R.id.radio3);
        radio4 = (RadioButton) findViewById(R.id.radio4);
        radio5 = (RadioButton) findViewById(R.id.radio5);
        radio6 = (RadioButton) findViewById(R.id.radio6);
        rb_time = (RadioGroup) findViewById(R.id.rb_time);
        rb_time.setOnCheckedChangeListener(this);
        //psts_list = (com.lst.projectlib.component.CustomView.PagerSlidingTabStrip) findViewById(R.id.psts_list);
        //vp_list = (NoScrollViewPager) findViewById(R.id.vp_list);
        tv_more = (TextView) findViewById(R.id.tv_more);
        tv_short_name = (TextView) findViewById(R.id.tv_short_name);
        tv_fill_name = (TextView) findViewById(R.id.tv_fill_name);
        tv_company = (TextView) findViewById(R.id.tv_company);
        tv_set_up_date = (TextView) findViewById(R.id.tv_set_up_date);
        tv_fund_number = (TextView) findViewById(R.id.tv_fund_number);
        tv_fund_type = (TextView) findViewById(R.id.tv_fund_type);
        tv_fund_state = (TextView) findViewById(R.id.tv_fund_state);
        tv_trading_state = (TextView) findViewById(R.id.tv_trading_state);
        tv_management_fee = (TextView) findViewById(R.id.tv_management_fee);
        tv_trusteeship_fee = (TextView) findViewById(R.id.tv_trusteeship_fee);
        tv_first_collect_scale = (TextView) findViewById(R.id.tv_first_collect_scale);
        tv_new_scale = (TextView) findViewById(R.id.tv_new_scale);
        tv_bank = (TextView) findViewById(R.id.tv_bank);
        tv_buy = (TextView) findViewById(R.id.tv_buy);
        tv_month1_1 = (TextView) findViewById(R.id.tv_month1_1);
        tv_month1_2 = (TextView) findViewById(R.id.tv_month1_2);
        tv_month1_3 = (TextView) findViewById(R.id.tv_month1_3);
        tv_month3_1 = (TextView) findViewById(R.id.tv_month3_1);
        tv_month3_2 = (TextView) findViewById(R.id.tv_month3_2);
        tv_month3_3 = (TextView) findViewById(R.id.tv_month3_3);
        tv_month6_1 = (TextView) findViewById(R.id.tv_month6_1);
        tv_month6_2 = (TextView) findViewById(R.id.tv_month6_2);
        tv_month6_3 = (TextView) findViewById(R.id.tv_month6_3);
        tv_year_1 = (TextView) findViewById(R.id.tv_year_1);
        tv_year_2 = (TextView) findViewById(R.id.tv_year_2);
        tv_year_3 = (TextView) findViewById(R.id.tv_year_3);
        tv_year1_1 = (TextView) findViewById(R.id.tv_year1_1);
        tv_year1_2 = (TextView) findViewById(R.id.tv_year1_2);
        tv_year1_3 = (TextView) findViewById(R.id.tv_year1_3);
        tv_year2_1 = (TextView) findViewById(R.id.tv_year2_1);
        tv_year2_2 = (TextView) findViewById(R.id.tv_year2_2);
        tv_year2_3 = (TextView) findViewById(R.id.tv_year2_3);
        tv_year3_1 = (TextView) findViewById(R.id.tv_year3_1);
        tv_year3_2 = (TextView) findViewById(R.id.tv_year3_2);
        tv_year3_3 = (TextView) findViewById(R.id.tv_year3_3);
        lin_year = (LinearLayout) findViewById(R.id.lin_year);
        tv_yifeng = (TextView) findViewById(R.id.tv_yifeng);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_financial_detail);
        bindViews();
        ttb_title_bar.findViewById(R.id.tvNavBack).setOnClickListener(this);
        tv_more.setOnClickListener(this);
        tv_favorite.setOnClickListener(this);
        tv_yifeng.setOnClickListener(this);
        lin_year.setVisibility(View.GONE);
        Drawable drawable = getResources().getDrawable(R.drawable.more);
        drawable.setBounds(0, 0, DensityUtil.dip2px(FinancialDetailActivity.this, 18), DensityUtil.dip2px(FinancialDetailActivity.this, 13));
        tv_more.setCompoundDrawables(null, null, drawable, null);
    }

    @Override
    protected void initData() {
        id = getIntent().getStringExtra("id");

        initWebView();
        getFundDetail(id);
        getFundPerformance(id);
        getMarketPerformance();
        if (UserTokenUtil.isLogin(this)) {
            isFavorited(id);
        }
    }

    private void isFavorited(String id) {
        FavoriteNetWork.isFavorited(UserTokenUtil.getToken(this), id, new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                if (msg != null && msg.getSuccess()) {
                    if (msg.getData() != null && msg.getData() instanceof Boolean) {
                        if ((boolean) msg.getData()) {
                            Drawable drawable = getResources().getDrawable(R.drawable.icon_shoucang_sel);
                            drawable.setBounds(0, 0, DensityUtil.dip2px(FinancialDetailActivity.this, 25), DensityUtil.dip2px(FinancialDetailActivity.this, 25));
                            tv_favorite.setCompoundDrawables(null, drawable, null, null);
                            tv_favorite.setText("已收藏");
                        } else {
                            Drawable drawable = getResources().getDrawable(R.drawable.icon_shoucang_dis);
                            drawable.setBounds(0, 0, DensityUtil.dip2px(FinancialDetailActivity.this, 25), DensityUtil.dip2px(FinancialDetailActivity.this, 25));
                            tv_favorite.setCompoundDrawables(null, drawable, null, null);
                            tv_favorite.setText("收    藏");
                        }
                    }
                }
            }

            @Override
            public void onCallBackError(Msg msg) {

            }
        });
    }

    private void getFundPerformance(String id) {
        JSONObject object = new JSONObject();
        try {
            object.put("fundCode", id);
            FundNetWork.getFundPerfromance(UserTokenUtil.getToken(FinancialDetailActivity.this), object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    if (msg != null && msg.getSuccess() && msg.getData() != null && msg.getData() instanceof List) {
                        List<PerformanceEntity> list = (List<PerformanceEntity>) msg.getData();
                        PerformanceEntity entity1 = list.get(1);
                        tv_month1_1.setText(StringAppUtil.isEmptyOrNull(entity1.getMonth1()) ? "--" : entity1.getMonth1() + "%");
                        tv_month3_1.setText(StringAppUtil.isEmptyOrNull(entity1.getMonth3()) ? "--" : entity1.getMonth3() + "%");
                        tv_month6_1.setText(StringAppUtil.isEmptyOrNull(entity1.getMonth6()) ? "--" : entity1.getMonth6() + "%");
                        tv_year_1.setText(StringAppUtil.isEmptyOrNull(entity1.getYearToDate()) ? "--" : entity1.getYearToDate() + "%");
                        tv_year1_1.setText(StringAppUtil.isEmptyOrNull(entity1.getYear1()) ? "--" : entity1.getYear1() + "%");
                        tv_year2_1.setText(StringAppUtil.isEmptyOrNull(entity1.getYear2()) ? "--" : entity1.getYear2() + "%");
                        tv_year3_1.setText(StringAppUtil.isEmptyOrNull(entity1.getYear3()) ? "--" : entity1.getYear3() + "%");
                        PerformanceEntity entity = list.get(0);
                        tv_month1_2.setText(StringAppUtil.isEmptyOrNull(entity.getMonth1()) ? "--" : entity.getMonth1() + "%");
                        tv_month3_2.setText(StringAppUtil.isEmptyOrNull(entity.getMonth3()) ? "--" : entity.getMonth3() + "%");
                        tv_month6_2.setText(StringAppUtil.isEmptyOrNull(entity.getMonth6()) ? "--" : entity.getMonth6() + "%");
                        tv_year_2.setText(StringAppUtil.isEmptyOrNull(entity.getYearToDate()) ? "--" : entity.getYearToDate() + "%");
                        tv_year1_2.setText(StringAppUtil.isEmptyOrNull(entity.getYear1()) ? "--" : entity.getYear1() + "%");
                        tv_year2_2.setText(StringAppUtil.isEmptyOrNull(entity.getYear2()) ? "--" : entity.getYear2() + "%");
                        tv_year3_2.setText(StringAppUtil.isEmptyOrNull(entity.getYear3()) ? "--" : entity.getYear3() + "%");
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getMarketPerformance() {
        JSONObject object = new JSONObject();
        try {
            object.put("indexCode", "000300.SH");
            FundNetWork.getMarketPerfromance(UserTokenUtil.getToken(FinancialDetailActivity.this), object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    if (msg != null && msg.getSuccess() && msg.getData() != null && msg.getData() instanceof PerformanceEntity) {
                        PerformanceEntity entity = (PerformanceEntity) msg.getData();
                        tv_month1_3.setText(StringAppUtil.isEmptyOrNull(entity.getMonth1()) ? "--" : entity.getMonth1() + "%");
                        tv_month3_3.setText(StringAppUtil.isEmptyOrNull(entity.getMonth3()) ? "--" : entity.getMonth3() + "%");
                        tv_month6_3.setText(StringAppUtil.isEmptyOrNull(entity.getMonth6()) ? "--" : entity.getMonth6() + "%");
                        tv_year_3.setText(StringAppUtil.isEmptyOrNull(entity.getYearToDate()) ? "--" : entity.getYearToDate() + "%");
                        tv_year1_3.setText(StringAppUtil.isEmptyOrNull(entity.getYear1()) ? "--" : entity.getYear1() + "%");
                        tv_year2_3.setText(StringAppUtil.isEmptyOrNull(entity.getYear2()) ? "--" : entity.getYear2() + "%");
                        tv_year3_3.setText(StringAppUtil.isEmptyOrNull(entity.getYear3()) ? "--" : entity.getYear3() + "%");
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {

                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getFundDetail(String id) {
        mDialog.show();
        JSONObject object = new JSONObject();
        try {
            object.put("fundCode", id);
            FundNetWork.getFundDetail(UserTokenUtil.getToken(FinancialDetailActivity.this), object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    if (msg != null && msg.getSuccess() && msg.getData() != null) {
                        mFund = (FundEntity) msg.getData();
                        setViews();
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void getFundChart(JSONObject json, final String period) {
        try {
            if (wv_chart != null && null != json) {
                wv_chart.loadUrl("javascript:initChart('" + json.toString() + "')");
                return;
            }

            if (!mDialog.isShowing())
                mDialog.show();

            JSONObject object = new JSONObject();
            object.put("fundCode", id);
            object.put("period", period);

            FundNetWork.getFundTypePerfromance(UserTokenUtil.getToken(FinancialDetailActivity.this), object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    List<Performance> list = (List<Performance>) msg.getData();
                    showChart(period, list);
                    mDialog.dismiss();
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void showChart(String period, List<Performance> list) {
        JSONObject jsonObject1 = new JSONObject();
        JSONObject jsonObject2 = new JSONObject();
        try {
            JSONArray legendArray1 = new JSONArray();
            JSONArray legendArray2 = new JSONArray();
            JSONArray xDataArray = new JSONArray();
            JSONArray yDataArrayTotal1 = new JSONArray();
            JSONArray yDataArrayTotal2 = new JSONArray();
            JSONArray yDataArray1 = new JSONArray();
            JSONArray yDataArray2 = new JSONArray();

            legendArray1.put(new JSONObject().put("legend", "MM".equals(type) ? "7日年化收益率(%)" : "收益走势(%)"));
            legendArray2.put(new JSONObject().put("legend", "MM".equals(type) ? "万份收益(元)" : "单位净值(元)"));
            jsonObject1.put("legend", legendArray1);
            jsonObject2.put("legend", legendArray2);
//            jsonObject1.put("legendShow", true);
//            jsonObject2.put("legendShow", true);

            for (Performance performance : list) {
                JSONObject xData = new JSONObject();
                xData.put("time", performance.getNavDate());
                JSONObject yData1 = new JSONObject();
                JSONObject yData2 = new JSONObject();
                yData1.put("data", String.valueOf(performance.getSevenDaysAnnualizedYield()));

                if ("MM".equals(type)) {
                    yData1.put("data", String.valueOf(performance.getSevenDaysAnnualizedYield()));
                    yData2.put("data", String.valueOf(performance.getEarningsPer10000()));
                } else {
                    yData1.put("data", String.valueOf(performance.getPerformance()));
                    yData2.put("data", String.valueOf(performance.getNav()));
                }

                xDataArray.put(xData);
                yDataArray1.put(yData1);
                yDataArray2.put(yData2);
            }

            yDataArrayTotal1.put(yDataArray1);
            yDataArrayTotal2.put(yDataArray2);

            jsonObject1.put("xData", xDataArray);
            jsonObject2.put("xData", xDataArray);
            jsonObject1.put("yData", yDataArrayTotal1);
            jsonObject2.put("yData", yDataArrayTotal2);

            if (rg_movements.getCheckedRadioButtonId() == rb_earnings_movements.getId())
                wv_chart.loadUrl("javascript:initChart('" + jsonObject1.toString() + "')");
            else
                wv_chart.loadUrl("javascript:initChart('" + jsonObject2.toString() + "')");

            if (period.equals("1")) {
                perjson_month = jsonObject1;
                navjson_month = jsonObject2;
            } else if (period.equals("2")) {
                perjson_3month = jsonObject1;
                navjson_3month = jsonObject2;
            } else if (period.equals("4")) {
                perjson_year = jsonObject1;
                navjson_year = jsonObject2;
            } else if (period.equals("5")) {
                perjson_3year = jsonObject1;
                navjson_3year = jsonObject2;
            } else if (period.equals("6")) {
                perjson_5year = jsonObject1;
                navjson_5year = jsonObject2;
            } else if (period.equals("8")) {
                perjson_history = jsonObject1;
                navjson_history = jsonObject2;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void setViews() {
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundName()))
            tv_name.setText(mFund.getFundName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getRiskRateName()))
            tv_risk.setText(mFund.getRiskRateName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundCode()))
            tv_number.setText(mFund.getFundCode());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundTypeName()))
            tv_type.setText(mFund.getFundTypeName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundType())) {
            type = mFund.getFundType();
            if ("MM".equals(type)) {
                rb_earnings_movements.setText("7日年化收益率");
                rb_value_movements.setText("万份收益");
            } else {
                rb_earnings_movements.setText("收益走势");
                rb_value_movements.setText("净值走势");
            }
        }
        if (!StringAppUtil.isEmptyOrNull(mFund.getNav()))
            tv_new_value.setText(mFund.getNav());
        if (!StringAppUtil.isEmptyOrNull(mFund.getDay1Performance()))
            tv_daily_increases.setText(mFund.getDay1Performance() + "%");
        if (StringAppUtil.isEmptyOrNull(mFund.getNavDate())) {
            tv_date.setText("");
        } else {
            String date = DateUtil.formatDate(Long.parseLong(mFund.getNavDate()), "yyyy-MM-dd");
            tv_date.setText(date);
        }
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundName()))
            tv_short_name.setText(mFund.getFundName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundFullName()))
            tv_fill_name.setText(mFund.getFundFullName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundHouse()))
            tv_company.setText(mFund.getFundHouse());
        if (StringAppUtil.isEmptyOrNull(mFund.getLaunchDate())) {
            tv_set_up_date.setText("");
        } else {
            String date = DateUtil.formatDate(Long.parseLong(mFund.getLaunchDate()), "yyyy-MM-dd");
            tv_set_up_date.setText(date);
        }
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundCode()))
            tv_fund_number.setText(mFund.getFundCode());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundTypeName()))
            tv_fund_type.setText(mFund.getFundTypeName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundStatusName()))
            tv_fund_state.setText(mFund.getFundStatusName());
        if (!StringAppUtil.isEmptyOrNull(mFund.getIsBuyEnable()))
            tv_trading_state.setText((mFund.getIsBuyEnable().equals("0") ? "可申购 " : "不可申购 ") + (mFund.getIsSellEnable().equals("0") ? "可赎回 " : "不可赎回 ") + (mFund.getIsRspEnable().equals("0") ? "可定投 " : "不可定投 "));
        if (!StringAppUtil.isEmptyOrNull(mFund.getManagementFee()))
            tv_management_fee.setText(mFund.getManagementFee() + "%");
        if (!StringAppUtil.isEmptyOrNull(mFund.getCustodianFee()))
            tv_trusteeship_fee.setText(mFund.getCustodianFee() + "%");
        if (!StringAppUtil.isEmptyOrNull(mFund.getLaunchSize()))
            tv_first_collect_scale.setText(mFund.getLaunchSize() + "亿元");
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundSize()))
            tv_new_scale.setText(mFund.getFundSize() + "亿元");
        if (!StringAppUtil.isEmptyOrNull(mFund.getFundCustodian()))
            tv_bank.setText(mFund.getFundCustodian());
        if ("Y".equals(getIntent().getStringExtra("state"))) {
            tv_buy.setText("立即购买");
            tv_buy.setBackgroundResource(R.drawable.sure_button_selector);
            tv_buy.setOnClickListener(this);
        } else if ("N".equals(getIntent().getStringExtra("state"))) {
            tv_buy.setText("已售罄");
            tv_buy.setBackgroundResource(R.color.black_999999);
        }
    }

    @SuppressLint("JavascriptInterface")
    private WebView initWebView() {
        wv_chart = (WebView) findViewById(R.id.wv_chart);
        WebSettings setting = wv_chart.getSettings();
        // 安全考虑
        // 密码不保存,默认会弹出对话框
        setting.setSavePassword(false);

        setting.setSupportZoom(true);//缩放开关
        setting.setBuiltInZoomControls(true);//设置是否可缩放
        setting.setDisplayZoomControls(false);

        //自适应屏幕
        setting.setUseWideViewPort(true);//设置webview推荐使用的窗口，设置为true，无限缩放，就需要使用大视图模式
        setting.setLoadWithOverviewMode(true);//设置webview加载的页面的模式，也设置为true。

        // 设置缓存
//		setting.setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
        // 取消缓存
        setting.setCacheMode(WebSettings.LOAD_NO_CACHE);

        setting.setAllowFileAccess(true);
        setting.setAppCacheEnabled(true);
//        setting.setAllowUniversalAccessFromFileURLs(true);

        // 支持javascript
        setting.setJavaScriptEnabled(true);
        wv_chart.addJavascriptInterface(this, "javatojs");

        //最重要的方法，一定要设置数据缓存，这就是出不来的主要原因
        setting.setDomStorageEnabled(true);
        //启用数据库
        setting.setDatabaseEnabled(true);

        //启用地理定位
        setting.setGeolocationEnabled(true);
        String dir = this.getApplicationContext().getDir("database", Context.MODE_PRIVATE).getPath();
        //设置定位的数据库路径
        setting.setGeolocationDatabasePath(dir);

        wv_chart.loadUrl("file:///android_asset/echarts.html");

        wv_chart.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                getFundChart(perjson_month, "1");
            }
        });
        return wv_chart;
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.rb_earnings_movements:
                if (rb_time.getCheckedRadioButtonId() == radio1.getId())
                    getFundChart(perjson_month, "1");
                else if (rb_time.getCheckedRadioButtonId() == radio2.getId())
                    getFundChart(perjson_3month, "2");
                else if (rb_time.getCheckedRadioButtonId() == radio3.getId())
                    getFundChart(perjson_year, "4");
                else if (rb_time.getCheckedRadioButtonId() == radio4.getId())
                    getFundChart(perjson_3year, "5");
                else if (rb_time.getCheckedRadioButtonId() == radio5.getId())
                    getFundChart(perjson_5year, "6");
                else if (rb_time.getCheckedRadioButtonId() == radio6.getId())
                    getFundChart(perjson_history, "8");
                break;
            case R.id.rb_value_movements:
                if (rb_time.getCheckedRadioButtonId() == radio1.getId())
                    getFundChart(navjson_month, "1");
                else if (rb_time.getCheckedRadioButtonId() == radio2.getId())
                    getFundChart(navjson_3month, "2");
                else if (rb_time.getCheckedRadioButtonId() == radio3.getId())
                    getFundChart(navjson_year, "4");
                else if (rb_time.getCheckedRadioButtonId() == radio4.getId())
                    getFundChart(navjson_3year, "5");
                else if (rb_time.getCheckedRadioButtonId() == radio5.getId())
                    getFundChart(navjson_5year, "6");
                else if (rb_time.getCheckedRadioButtonId() == radio6.getId())
                    getFundChart(navjson_history, "8");
                break;
            case R.id.radio1:
                if (rg_movements.getCheckedRadioButtonId() == rb_earnings_movements.getId())
                    getFundChart(perjson_month, "1");
                else
                    getFundChart(navjson_month, "1");
                break;
            case R.id.radio2:
                if (rg_movements.getCheckedRadioButtonId() == rb_earnings_movements.getId())
                    getFundChart(perjson_3month, "2");
                else
                    getFundChart(navjson_3month, "2");
                break;
            case R.id.radio3:
                if (rg_movements.getCheckedRadioButtonId() == rb_earnings_movements.getId())
                    getFundChart(perjson_year, "4");
                else
                    getFundChart(navjson_year, "4");
                break;
            case R.id.radio4:
                if (rg_movements.getCheckedRadioButtonId() == rb_earnings_movements.getId())
                    getFundChart(perjson_3year, "5");
                else
                    getFundChart(navjson_3year, "5");
                break;
            case R.id.radio5:
                if (rg_movements.getCheckedRadioButtonId() == rb_earnings_movements.getId())
                    getFundChart(perjson_5year, "6");
                else
                    getFundChart(navjson_5year, "6");
                break;
            case R.id.radio6:
                if (rg_movements.getCheckedRadioButtonId() == rb_earnings_movements.getId())
                    getFundChart(perjson_history, "8");
                else
                    getFundChart(navjson_history, "8");
                break;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvNavBack:
                back();
                break;
            case R.id.tv_yifeng:
                showPopupWindow(tv_yifeng);
                break;
            case R.id.tv_favorite:
                if (UserTokenUtil.isLogin(FinancialDetailActivity.this)) {
                    if ("已收藏".equals(tv_favorite.getText().toString())) {
                        delFavorite();
                    } else {
                        addFavorite();
                    }
                } else {
                    Intent intent = new Intent(FinancialDetailActivity.this, LoginActivity.class);
                    startActivityForResult(intent, 3);
                }
                break;
            case R.id.tv_more:
                if (lin_year.getVisibility() == View.VISIBLE) {
                    lin_year.setVisibility(View.GONE);
                    tv_more.setText("查看更多");
                    Drawable drawable = getResources().getDrawable(R.drawable.more);
                    drawable.setBounds(0, 0, DensityUtil.dip2px(FinancialDetailActivity.this, 18), DensityUtil.dip2px(FinancialDetailActivity.this, 13));
                    tv_more.setCompoundDrawables(null, null, drawable, null);
                } else {
                    lin_year.setVisibility(View.VISIBLE);
                    tv_more.setText("收起");
                    Drawable drawable = getResources().getDrawable(R.drawable.shouqi);
                    drawable.setBounds(0, 0, DensityUtil.dip2px(FinancialDetailActivity.this, 18), DensityUtil.dip2px(FinancialDetailActivity.this, 13));
                    tv_more.setCompoundDrawables(null, null, drawable, null);
                }
                break;
            case R.id.tv_buy:
                if (UserTokenUtil.isLogin(FinancialDetailActivity.this)) {
                    if (UserTokenUtil.isOpenAccount(FinancialDetailActivity.this)) {
                        MyAlertDialog alertDialog = new MyAlertDialog(FinancialDetailActivity.this, "您将使用奕丰金融提供的基金销售服务", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {

                                Intent intent = new Intent(FinancialDetailActivity.this, FinancialBuyActivity.class);
                                intent.putExtra("id", mFund.getFundCode());
                                startActivity(intent);
                            }
                        });
                        alertDialog.show();
                    } else {
                        ToastUtils.showShortToast(FinancialDetailActivity.this, "请先开户");
                        Intent intent = new Intent(FinancialDetailActivity.this, OpenAccountActivity.class);
                        startActivity(intent);
                    }
                } else {
                    Intent intent = new Intent(FinancialDetailActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
                break;
        }
    }

    private void addFavorite() {
        mDialog.show();
        JSONObject object = new JSONObject();
        try {
            object.put("FundCode", mFund.getFundCode());
            object.put("FundName", mFund.getFundName());
            FavoriteNetWork.addFavorite(UserTokenUtil.getToken(this), object.toString(), new CallBackListener() {
                @Override
                public void onCallBackCompleted(Msg msg) {
                    mDialog.cancel();
                    if (msg != null && msg.getSuccess()) {
                        ToastUtils.showShortToast(FinancialDetailActivity.this, "收藏成功");
                        Drawable drawable = getResources().getDrawable(R.drawable.icon_shoucang_sel);
                        drawable.setBounds(0, 0, DensityUtil.dip2px(FinancialDetailActivity.this, 25), DensityUtil.dip2px(FinancialDetailActivity.this, 25));
                        tv_favorite.setCompoundDrawables(null, drawable, null, null);
                        tv_favorite.setText("已收藏");
                    } else {
                        if (TextUtils.isEmpty(msg.getMsg()) && "null".equals(msg.getMsg())) {
                            ToastUtils.showShortToast(FinancialDetailActivity.this, "收藏失败");
                        } else {
                            ToastUtils.showShortToast(FinancialDetailActivity.this, msg.getMsg());
                        }
                    }
                }

                @Override
                public void onCallBackError(Msg msg) {
                    mDialog.cancel();
                }
            });
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    private void delFavorite() {
        mDialog.show();
        FavoriteNetWork.delFavorite(UserTokenUtil.getToken(this), mFund.getFundCode(), new CallBackListener() {
            @Override
            public void onCallBackCompleted(Msg msg) {
                mDialog.cancel();
                if (msg != null && msg.getSuccess()) {
                    ToastUtils.showShortToast(FinancialDetailActivity.this, "取消收藏成功");
                    Drawable drawable = getResources().getDrawable(R.drawable.icon_shoucang_dis);
                    drawable.setBounds(0, 0, DensityUtil.dip2px(FinancialDetailActivity.this, 25), DensityUtil.dip2px(FinancialDetailActivity.this, 25));
                    tv_favorite.setCompoundDrawables(null, drawable, null, null);
                    tv_favorite.setText("收    藏");
                } else {
                    if (TextUtils.isEmpty(msg.getMsg()) && "null".equals(msg.getMsg())) {
                        ToastUtils.showShortToast(FinancialDetailActivity.this, "取消收藏失败");
                    } else {
                        ToastUtils.showShortToast(FinancialDetailActivity.this, msg.getMsg());
                    }
                }
            }

            @Override
            public void onCallBackError(Msg msg) {
                mDialog.cancel();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 3 && resultCode == 111) {
            isFavorited(id);
        }
    }
}
